<?php

namespace Admin\Model\DAO;

use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Where;
use Admin\Entity\Article;

class ArticleTable extends AbstractTableGateway {

    protected $table = 'lib_articles';
    protected $adapter;
    protected $resultSetPrototype;

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
    }

    public function getArticle($articleID) {
        $articleID = (int) $articleID;
        $rowset = $this->select(array('articleID' => $articleID));
        $row = $rowset->current();
        if (!$row) {
            return null;
        }
        return $row;
    }

    public function fetchAll() {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->from(array('a' => $this->table))
                ->join(array('sca' => 'lib_subcategoryarticle'), 'a.subCategoryID = sca.subCategoryID', array('subCategoryName'))
                ->join(array('ca' => 'lib_categoryarticle'), 'ca.categoryID = sca.categoryID', array('categoryName'));
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);
        $resultSet->buffer();
        $resultSet->next();
        return $resultSet;
    }

    public function getAllArticlePublish($isPublisher, $subCategoryID) {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->from(array('a' => $this->table));
        $where = new Where();
        $where->equalTo('a.isPublisher', $isPublisher)
                ->AND
                ->equalTo('a.subCategoryID', $subCategoryID);
        $select->where($where);
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);
        $resultSet->buffer();
        $resultSet->next();
        return $resultSet;
    }
    public function getNewArticle($isPublisher, $subCategoryID, $numberArticle) {
         $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->from(array('a' => $this->table));
        $where = new Where();
        $where->equalTo('a.isPublisher', $isPublisher)
                ->AND
                ->equalTo('a.subCategoryID', $subCategoryID);
        $select->where($where)
                ->order(array('dateCreated'))
                ->limit($numberArticle);
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);
        $resultSet->buffer();
        $resultSet->next();
        return $resultSet;
    }
        public function getListNewArticle($isPublisher, $subCategoryID, $numberArticle, $dateCreated) {
         $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->from(array('a' => $this->table));
        $where = new Where();
        $where->equalTo('a.isPublisher', $isPublisher)
                ->AND
                ->equalTo('a.subCategoryID', $subCategoryID)
                ->AND
                ->notEqualTo('a.dateCreated', $dateCreated);
        $select->where($where)
                ->order(array('dateCreated'))
                ->limit($numberArticle);
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);
        $resultSet->buffer();
        $resultSet->next();
        return $resultSet;
    }
    public function saveArticle(Article $article) {
        $dataArray = $article->getArrayCopy();
        if ($this->insert($dataArray)) {
            return true;
        }
        return false;
    }

    public function updateArticle(Article $article, $articleID) {
        $dataArray = array(
            'title' => $article->title,
            'dateCreated' => $article->dateCreated,
            'isPublisher' => $article->isPublisher,
            'pictureThumnail' => $article->pictureThumnail,
            'description' => $article->description,
            'contentDetail' => $article->contentDetail,
            'subCategoryID' => $article->subCategoryID,
        );
        if ($this->update($dataArray, array('articleID' => $articleID))) {
            return true;
        }
        return false;
    }

    public function deleteArticle($articleID) {
        $this->delete(array('articleID' => $articleID));
    }

    public function changeStatusPublishArticle($articleID, $status) {
        $this->update(array('isPublisher' => $status), array('articleID' => $articleID));
    }

}

?>
